﻿/* ==============================================================================
   * 文 件 名:DbFactory
   * 功能描述：
   * Copyright (c) 2013 
   * 创 建 人: mashanlin
   * 创建时间: 2013/5/5 15:27:47
   * 修 改 人: 
   * 修改时间: 
   * 修改描述: 
   * 版    本: v1.0.0.0
   * ==============================================================================*/
using System;
using System.Collections.Generic;

using System.Text;
using System.Reflection;
using MSL.DataBase.Core;
using System.Data.Common;
using MSL.DataBase.Lib;

namespace MSL.DataBase
{
    /// <summary>
    /// 数据库访问工厂类
    /// </summary>
    internal class DbFactory
    {
        internal static IDbHelper helper;
        private static object locker = new Object();

        /// <summary>
        /// 获取实例
        /// </summary>
        /// <param name="connString">数据库链接</param>
        /// <param name="dbClass">数据库操作类</param>
        /// <returns></returns>
        private static IDbHelper GetHelper(string connString, string dbClass)
        {
            if (helper != null && (connString + "").Length != 0 && !connString.Equals(DBConfig.ConnString))
            {
                helper = null;
            }
            if ((connString + "").Length != 0)
            {
                dbClass = DBConfig.GetHelperClass(DBConfig.GetPovider(connString));
            }
            switch (dbClass)
            {
                case "DataBase.Core.SQLHelper":
                    helper = new SQLHelper(connString);
                    break;
                case "DataBase.Core.OleHelper":
                    helper = new OleHelper(connString);
                    break;
                case "DataBase.Core.SQLiteHelper":
                    helper = new SQLiteHelper(connString);
                    break;
                case "DataBase.Core.MySqlHelper":
                    helper = new MySqlHelper(connString);
                    break;
                case "DataBase.Core.OracleHelper":
                    helper = new OracleHelper(connString);
                    break;
                default:
                    helper = new SQLHelper(connString);
                    break;
            }
            return helper;
        }
        /// <summary>
        /// 获取实例
        /// </summary>
        /// <param name="connString">数据库链接</param>
        /// <returns></returns>
        internal static IDbHelper GetHelper(string connString)
        {
            //数据库连接与默认的不同
            if (helper != null && (connString + "").Length != 0)//&& !connString.Equals(DBConfig.ConnString))
            {
                helper = null;
            }
            if (helper == null)
            {
                lock (locker)
                {
                    if (helper == null)
                    {
                        //创建有参数的构造函数
                        if ((connString + "").Length == 0)
                        {
                            //测试通过
                            //helper = (IDbHelper)Assembly.Load("DataBase").CreateInstance(DBConfig.DbHelperClass, true, new System.Reflection.BindingFlags(), null, new object[] { DBConfig.ConnString }, null, null);

                            helper = GetHelper(null, DBConfig.DbHelperClass);
                        }
                        else
                        {
                            //helper = (IDbHelper)Assembly.Load("DataBase").CreateInstance(DBConfig.GetHelperClass(DBConfig.GetPovider(connString)), true, new System.Reflection.BindingFlags(), null, new object[] { connString }, null, null);
                            helper = GetHelper(connString, null);
                        }
                    }
                }
            }

            return helper;
        }
    }
}
